package cn.doitedu.eagle.ruleutils;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/***
 * @author hunter.d
 * @qq 657270652
 * @wx haitao-duan
 * @date 2021/3/14
 **/
public class Rule2Mysql {
    public static void main(String[] args) throws Exception {

        String ruleId = "triggerTest2";
        String ruleCode = FileUtils.readFileToString(new File("realtime_rule/rules/"+ruleId+".drl"), "utf-8");
        String cntSql = "";
        String seqSql = "";


        String cntSqlFileName = "realtime_rule/rules/" + ruleId + "-cnt.sql";
        String seqSqlFileName = "realtime_rule/rules/" + ruleId + "-seq.sql";
        System.out.println(cntSqlFileName);
        System.out.println(seqSqlFileName);
        File cntSqlFile = new File(cntSqlFileName);
        File seqSqlFile = new File(seqSqlFileName);
        if(cntSqlFile.exists()){
            cntSql = FileUtils.readFileToString(cntSqlFile,"utf-8");
        }
        if(seqSqlFile.exists()){
            seqSql = FileUtils.readFileToString(seqSqlFile,"utf-8");
        }

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://hdp01:3306/realtimedw?useUnicode=true&characterEncoding=utf8", "root", "ABC123abc.123");
        String sql = "insert into canal_rule (rule_name,rule_code,rule_type,rule_status,eventCountQuerySqls,eventSeqQuerySql,create_time,modify_time) values (?,?,?,?,?,?,?,?)";
        PreparedStatement st = conn.prepareStatement(sql);
        st.setString(1,ruleId);
        st.setString(2,ruleCode);
        st.setString(3,"0");
        st.setString(4,"0");
        st.setString(5,cntSql);
        st.setString(6,seqSql);
        st.setString(7,"2021-03-16 12:24:30");
        st.setString(8,"2021-03-16 12:24:30");
        st.execute();

        conn.close();


    }

}
